import { ConfigModule, ConfigService, Utils } from '../../@nt';
import { RedisModule, RedisModuleOptions } from '@liaoliaots/nestjs-redis';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CacheModule } from '@nestjs/cache-manager';
// import { v4 as uuidv4 } from 'uuid';

// const generateString = () => uuidv4();

// 连接redis数据库(异步)
export const RedisAsyncConfig: any = RedisModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: async (
    configService: ConfigService,
  ): Promise<RedisModuleOptions> => {
    return {
      closeClient: true,
      config: {
        url: Utils.CryptoJS.decodeBase64(configService.get('REDIS_URI')),
      },
    };
  },
});

export const MysqlRemoteConfig = TypeOrmModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (config: ConfigService) => ({
    ...JSON.parse(Utils.CryptoJS.decodeBase64(config.get('MYSQL_URI'))),
    synchronize: false,
    entities: [__dirname + '/../../module/*'],
    charset: 'UTF8MB4_UNICODE_CI',
  }),
  inject: [ConfigService],
});

// export const MysqlRemoteConfig = TypeOrmModule.forRootAsync({
//   imports: [ConfigModule],
//   useFactory: (configService: ConfigService) => ({
//     type: 'mysql',
//     host: configService.get('HOST'),
//     port: +configService.get('PORT'),
//     username: configService.get('USERNAME'),
//     password: configService.get('PASSWORD'),
//     database: configService.get('DATABASE'),
//     entities: [],
//     synchronize: true,
//   }),
//   inject: [ConfigService],
// });

// 全局缓存（只能适合GET）-内存数据，不经过controller，直接返回数据，数据会在设置时间内自动删除（和typeorm设置一样）
export const CacheManageConfig = CacheModule.register({
  ttl: 1, // seconds // 到期时间
  max: 10, // seconds
});
